package com.cqs.leetcode.offerII;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;

import static java.util.stream.Collectors.toList;

public class OfferII004 {

    //位运算生疏
    public int singleNumber(int[] nums) {
        int result = 0;
        for (int i = 0; i < 32; i++) {
            int bit = 0;
            for (int num : nums) {
                //从低位向高位
                bit += (num >> i) & 1;
            }
            bit = bit % 3;
            result += bit << i ;
        }
        return result;
    }


    public static void main(String[] args) {
        List<int[]> list = new ArrayList<>();
        list.add(new int[]{8, 8, 8, 11, 34, 34, 34});
        list.add(new int[]{Integer.MIN_VALUE, 8, 8, 8, 34, 34, 34});
        list.add(new int[]{Integer.MAX_VALUE, 8, 8, 8, 34, 34, 34});
        OfferII004 o = new OfferII004();
        list.forEach(new Consumer<int[]>() {
            @Override
            public void accept(int[] ints) {
                List<Integer> collect = Arrays.stream(ints).boxed().collect(toList());
                System.out.println(collect + "\t" + o.singleNumber(ints));
            }
        });


    }
}
